#include "cdynamicguiobject.h"

/**
 * init - initialization
 */
void CDynamicGuiObject::init() {
  CGuiObject::init();
  this->fontId = -1;
}
//---------------------------------------------------------------------------

/**
 * getStyleNormal - returns style of normal state
 * @return style of normal component state
 */
CGuiStyle CDynamicGuiObject::getStyleNormal() const {
  return styleNormal;
}
//---------------------------------------------------------------------------

/**
 * getStylePrelight - returns style of prelighted state
 * @return style of prelighted component state
 */
CGuiStyle CDynamicGuiObject::getStylePrelight() const {
  return stylePrelight;
}
//---------------------------------------------------------------------------

/**
 * getStyleFocus - returns style of active state
 * @return style of active component state
 */
CGuiStyle CDynamicGuiObject::getStyleFocus() const {
  return styleFocus;
}
//---------------------------------------------------------------------------

/**
 * getStyleDisabled - returns style of disabled state
 * @return style of disabled component state
 */
CGuiStyle CDynamicGuiObject::getStyleDisabled() const {
  return styleDisabled;
}
//---------------------------------------------------------------------------

/**
 * getFontId - returns id of font used by the component
 * @return font id
 */
int CDynamicGuiObject::getFontId() const {
  return fontId;
}
//---------------------------------------------------------------------------

/**
 * setStyleNormal - sets style of component for normal state
 * @param styleNormal
 */
void CDynamicGuiObject::setStyleNormal(CGuiStyle styleNormal) {
  this->styleNormal = styleNormal;
  this->setStyle(styleNormal);
}
//---------------------------------------------------------------------------

/**
 * setStylePrelight - sets style of component for prelighted state
 * @param stylePrelight
 */
void CDynamicGuiObject::setStylePrelight(CGuiStyle stylePrelight) {
  this->stylePrelight = stylePrelight;
}
//---------------------------------------------------------------------------

/**
 * setStyleFocus - sets style of component for active state
 * @param styleFocus
 */
void CDynamicGuiObject::setStyleFocus(CGuiStyle styleFocus) {
  this->styleFocus = styleFocus;
}
//---------------------------------------------------------------------------

/**
 * setStyleDisabled - sets style of component for disabled state
 * @param styleDisabled
 */
void CDynamicGuiObject::setStyleDisabled(CGuiStyle styleDisabled) {
  this->styleDisabled = styleDisabled;
}
//---------------------------------------------------------------------------

/**
 * setDisabled - sets if component is disabled
 * @param disabled
 */
void CDynamicGuiObject::setDisabled(bool disabled) {
  this->disabled = disabled;
  style = disabled ? styleDisabled : styleNormal;
}
//---------------------------------------------------------------------------

/**
 * setFontId - sets id of font, used by the component
 * @param fontId
 */
void CDynamicGuiObject::setFontId(int fontId) {
  this->fontId = fontId;
}
//---------------------------------------------------------------------------

/**
 * setStyle - sets current style of component
 * @param style
 */
void CDynamicGuiObject::setStyle(CGuiStyle style) {
  this->style = style;
}
//---------------------------------------------------------------------------

/**
 * fitTextToWidth - attempts to fit given text on component
 * @param text
 * @param textWidth
 * @param maxWidth
 * @param rendererPtr
 * @return true/false text fits/does not fit on the component
 */
bool CDynamicGuiObject::fitTextToWidth(string &text,
    unsigned short &textWidth, unsigned short maxWidth,
    CGuiRenderer* rendererPtr) {

  // If text width is greater than maximum allowed
  string tmp(text);
  if (textWidth > maxWidth) {

    // Truncate text until it fits
    tmp = tmp.length() > 4 ? tmp.substr(0, tmp.length() - 4) : tmp;
    while (tmp.length() && (textWidth = rendererPtr->calculateTTFWidth(
        fontId, tmp + "...")) > maxWidth) {
      tmp = tmp.substr(0, tmp.length() - 1);
    }

    // If text still does not fit, return
    // Otherwise save truncated text
    if (textWidth > maxWidth) {
      return false;
    } else {
      text = tmp + "...";
    }
  }
  return true;
}
//---------------------------------------------------------------------------

/**
 * findHighestDynamicGuiObjectOwnerPtr - finds component of the highest level
 * @return pointer on the component of the highest level, which is not dialog
 */
CDynamicGuiObject* CDynamicGuiObject::findHighestDynamicGuiObjectOwnerPtr() {
  CDynamicGuiObject* tmpPtr = this;
  while (dynamic_cast<CDynamicGuiObject*>(tmpPtr->getOwnerPtr())) {
    tmpPtr = dynamic_cast<CDynamicGuiObject*>(tmpPtr->getOwnerPtr());
  }
  return tmpPtr;
}
//---------------------------------------------------------------------------
